<?php
session_start();

include '../conexao.php';

/*
 * Mostra o formulário de alteração dos dados do paciente
 */
if (isset($_GET['despId']))
{
	/*
	 * Consulta o despesa.
	 */
	$rs = mysql_query("SELECT * FROM `DESPESA` WHERE `ID_DESPESA` = " . $_GET['despId'])
		or die("ERRO no comando SQL:" . mysql_error());

	$arrayFields = mysql_fetch_array($rs);

	$idEstab = $arrayFields['ESTABELECIMENTO_ID_ESTABELECIMENTO'];
	$idPac = $arrayFields['PACIENTE_ID_PACIENTE'];
	$idTipoDesp = $arrayFields['TIPO_DESPESA_ID_TIPO_DESPESA'];
	$dataDesp = $arrayFields['DATA_DESP'];
	$valorDesp = $arrayFields['VALOR'];
	$obs = $arrayFields['OBSERVACAO'];
	
	/*
	 * Consulta o paciente referente a despesa.
	 */
	$rs = mysql_query("SELECT * FROM `PACIENTE` WHERE `ID_PACIENTE` = " . $idPac)
		or die("ERRO no comando SQL:" . mysql_error());

	$arrayFields = mysql_fetch_array($rs);
	
	$nomePac = $arrayFields['NOME'];
	
	/*
	 * Consulta o estabelecimento referente a despesa.
	 */
	$rs = mysql_query("SELECT * FROM `ESTABELECIMENTO` WHERE `ID_ESTABELECIMENTO` = " . $idEstab)
		or die("ERRO no comando SQL:" . mysql_error());

	$arrayFields = mysql_fetch_array($rs);
	
	$nomeEstab = $arrayFields['NOME_FANTASIA'];
	
	/*
	 * Consulta o tipo_despesa referente a despesa.
	 */
	$rs = mysql_query("SELECT * FROM `TIPO_DESPESA` WHERE `ID_TIPO_DESPESA` = " . $idTipoDesp)
		or die("ERRO no comando SQL:" . mysql_error());

	$arrayFields = mysql_fetch_array($rs);
	
	$tipoDesp = $arrayFields['DESCRICAO'];
}
?>
<?php
	if($_GET['acao']=='novo')
		$proximaAcao='inserir';
	else
		$proximaAcao='atualizar';
?>
<form name="cadDesp" id="cadDesp" action="despesa/acao.php" onsubmit="return validateForm(this)" method="POST">
	<input type="hidden" name="acao" value="<?=$proximaAcao?>">
	<input type="hidden" name="despId" value="<?=$_GET['despId']?>">
	<input type="hidden" name="idEstab" value="<?=$idEstab?>">
	<input type="hidden" name="idPac" value="<?=$idPac?>">
	<input type="hidden" name="idTipoDesp" value="<?=$idTipoDesp?>">
	<table align="left" cellpadding="2" cellspacing="5">
	<tr>
		<td>
			<table align="left" cellpadding="2" cellspacing="5">
				<tr>
					<td class="abutton">
						<a href="javascript:void(1);" onclick="javascript:carregaAjax('page', 'despesa/listaDesp.php');">Voltar</a>
						| <a href="javascript:void(1);" onclick="submitForm(document.forms['cadDesp'],'<?=$proximaAcao?>');">Salvar</a>
					</td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td>
			<table class="tableBorderCad" frame="hsides" align="left" cellpadding="2" cellspacing="5">
				<tr>
					<td colspan="4"><b>Estabelecimento:</b><br>
					<input disabled="disabled" type="text" style="width: 80%" name="estab" value="<?=$nomeEstab?>"/>
					<input type="button" name="procEstab" value="..." onclick="javascript:openDiv('estabelecimento/pesquisaEstab.php?formName=cadDesp');"/>
					</td>
				</tr>
				<tr>
					<td colspan="4"><b>Paciente:</b><br>
					<input disabled="disabled" type="text" style="width: 80%" name="paciente" value="<?=$nomePac?>"/>
					<input type="button" name="procPac" value="..." onclick="javascript:openDiv('paciente/pesquisaPac.php?formName=cadDesp');"/>
					</td>
				</tr>
				<tr>
					<td colspan="4"><b>Tipo Despesa:</b><br>
					<input disabled="disabled" type="text" style="width: 80%" name="tipoDesp" value="<?=$tipoDesp?>"/>
					<input type="button" name="procTipoDesp" value="..." onclick="javascript:openDiv('tipoDespesa/pesquisaTipoDesp.php?formName=cadDesp');"/>
					</td>
				</tr>
				<tr>
					<td colspan="4"><b>Data Despesa:</b><br>
                                        <!--  -->
                                        <input type="text" id="dataDesp" name="dataDesp" onClick="show_calendar('cadDesp.dataDesp');" value="<?=$dataDesp?>" onkeypress="mascara(this,mdata)" maxlength="10" />
					</td>
				</tr>
				<tr>
					<td colspan="4"><b>Valor:</b><br>
					<input type="text" style="width: 50%" name="valorDesp" value="<?=$valorDesp?>" onkeypress="mascara(this,soValor)" maxlength="10" />
					</td>
				</tr>
				<tr>
					<td colspan="4">
						Observa&ccedil;&otilde;es:<br>
<textarea style="width: 80%" rows="5" cols="45" name="obs">
<?=trim($obs)?>
</textarea>
					</td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td>
			<table align="left" cellpadding="2" cellspacing="5">
				<tr>
					<td class="abutton">
						<a href="javascript:void(1);" onclick="javascript:carregaAjax('page', 'despesa/listaDesp.php');">Voltar</a>
						| <a href="javascript:void(1);" onclick="submitForm(document.forms['cadDesp'],'<?=$proximaAcao?>');">Salvar</a>
					</td>
				</tr>
			</table>
		</td>
	</tr>
        </table>
</form>
